package com.viewpagertext.fragments;

import static com.viewpagertext.adapters.MusicGridAdapter.FindImgUrlPath;

import android.content.Intent;

import androidx.databinding.DataBindingUtil;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.viewpagertext.DBHelper.AppDatabase;
import com.viewpagertext.DBHelper.Dao.RecentPlayDao;
import com.viewpagertext.Model.RecentPlay;
import com.viewpagertext.R;
import com.viewpagertext.activitys.LocalActivity;
import com.viewpagertext.activitys.LocalMusicActivity;
import com.viewpagertext.activitys.PlayActivity;
import com.viewpagertext.activitys.RecentPlayActivity;
import com.viewpagertext.constructor.MessageEvent;
import com.viewpagertext.databinding.FragmentMyBinding;
import com.viewpagertext.json.StarredSong;

import org.greenrobot.eventbus.EventBus;

import java.util.List;
import java.util.Objects;

import static com.viewpagertext.adapters.MusicGridAdapter.MyStarredSong;
import static com.viewpagertext.adapters.MusicGridAdapter.RecentPlayFlag;



public class MyFragment extends Fragment {

    FragmentMyBinding binding;

    RecentPlayDao helper = AppDatabase.getInstance().recentPlayDao();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_my, container, false);
        initView();
        return binding.getRoot();
    }

    private void initView() {
        binding.loadMusic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), LocalMusicActivity.class));
            }
        });

        binding.recentPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), PlayActivity.class);
                RecentPlayFlag = true;
                List<RecentPlay> recent = helper.getAllRecentPlay();
                System.out.println("大小" + recent.size());
                RecentPlay song = helper.getFirstSong();
                if (song != null)
                    FindImgUrlPath = song.pic;
                String ft = "最近播放";
                EventBus.getDefault().postSticky(new MessageEvent(ft));//发送
                Objects.requireNonNull(getActivity()).startActivity(intent);

            }
        });
    }
}
